﻿using System;
using System.Threading;
using Quartz.Core;
using ZilLion.TaskManager.Core.Infrastructure;

namespace ZilLion.TaskManager.Core.SystemMonitor
{
    /// <summary>
    ///     基础监控者
    /// </summary>
    public abstract class BaseMonitor
    {
        private bool _isrunning;
        protected Thread Thread;

        public BaseMonitor()
        {
            _isrunning = true;
            Thread = new Thread(TryRun) {IsBackground = true};
            Thread.Start();
        }

        /// <summary>
        ///     监控间隔时间 （毫秒）
        /// </summary>
        public virtual int Interval { get; set; }

        private void TryRun()
        {
            while (_isrunning)
                try
                {
                    Monitor();
                    Thread.Sleep(Interval);
                }
                catch (Exception exp)
                {
                    CustomTaskLogger.Print(exp, GetType().Name + "监控严重错误");
                }
        }

        /// <summary>
        ///     监控执行方法约定
        /// </summary>
        protected virtual void Monitor()
        {
        }

        /// <summary>
        ///     通知监控执行方法约定
        /// </summary>
        public virtual void Stop()
        {
            _isrunning = false;
        }
    }
}